package org.dg.iag.query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.logging.Logger;

import org.dg.iag.util.ApplicationConstants;

public class XQueryGet {
	
	public Logger logger	= Logger.getLogger(XQueryGet.class.getName());
	
	private String baseUrl	= null;
	private String query	= null;	
	private Boolean wrap	= null;
	private Boolean indent	= false;
	private String customWrap						= null;
	private HashMap<String, String> filterParams	= null;
	private Integer start		= null;
	private Integer howmany		= null;
	
	public XQueryGet(String baseUrl, String query, Boolean wrap) {
		super();
		this.baseUrl = baseUrl;
		this.query = query;
		this.wrap = wrap;
	}
	
	public XQueryGet(String baseUrl, String query, Boolean wrap, Boolean indent) {
		super();
		this.baseUrl = baseUrl;
		this.query = query;
		this.wrap = wrap;
		this.indent = indent;
	}
	
	

	public XQueryGet(String baseUrl, String query, Boolean wrap,
			Boolean indent, String customWrap) {
		super();
		this.baseUrl = baseUrl;
		this.query = query;
		this.wrap = wrap;
		this.indent = indent;
		this.customWrap = customWrap;
	}
	
	
	public XQueryGet(String baseUrl, String query, Boolean wrap,
			Boolean indent, String customWrap,
			HashMap<String, String> filterParams) {
		super();
		this.baseUrl = baseUrl;
		this.query = query;
		this.wrap = wrap;
		this.indent = indent;
		this.customWrap = customWrap;
		this.filterParams = filterParams;
	}

	/**
	 * @return the baseUrl
	 */
	public String getBaseUrl() {
		return baseUrl;
	}
	/**
	 * @param baseUrl the baseUrl to set
	 */
	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}
	/**
	 * @return the query
	 */
	public String getQuery() {
		return query;
	}
	/**
	 * @param query the query to set
	 */
	public void setQuery(String query) {
		this.query = query;
	}
	/**
	 * @return the wrap
	 */
	public Boolean getWrap() {
		return wrap;
	}
	/**
	 * @param wrap the wrap to set
	 */
	public void setWrap(Boolean wrap) {
		this.wrap = wrap;
	}
	
	
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getHowmany() {
		return howmany;
	}

	public void setHowmany(int howmany) {
		this.howmany = howmany;
	}

	@Override
	public String toString() {
		
		List<String> params		= new ArrayList<String>();
		String processedQuery	= this.query;
		if ( filterParams != null && filterParams.size() > 0 ) {
			processedQuery		+= XQueryUtil.generateXpathCondition(filterParams, true);
		}
		if ( this.customWrap != null && this.customWrap.length() > 0 ) {
			processedQuery	= "let $ret := " + processedQuery + 
					" return <" + this.customWrap + ">{$ret}</" + this.customWrap + ">";
		}
		if ( this.query != null && this.query.length() != 0 ) {
			params.add( "_query=" + processedQuery );
		}
		if ( this.wrap != null ) {
			params.add(  (this.wrap?"_wrap=yes":"_wrap=no") );
		}
		if ( this.indent != null ) {
			params.add( this.indent?"_indent=yes":"_indent=no" );
		}
		if ( this.start != null )
			params.add("_start="+this.start);
		if ( this.howmany != null ) {
			params.add("_howmany=" + (this.howmany<ApplicationConstants.MAX_ITEMS_IN_QUERY?this.howmany:ApplicationConstants.MAX_ITEMS_IN_QUERY) );
		}
		if ( params.size() == 0 )
			return this.baseUrl;
		if ( params.size() == 1 ) 
			return this.baseUrl + "?" + params.get(0);
		
		String paramsString	= "";
		for (int i = 0; i < params.size(); i++) {
			paramsString += params.get(i);
			if ( i != params.size()-1 ) {
				paramsString += "&";
			} 
		}
		
		logger.info( "Generating get query: " + this.baseUrl + "?" + paramsString );
		return this.baseUrl + "?" + paramsString;
	}
}
